const config = require("../config");
const db = require("../db");
const bcrypt = require('bcryptjs')
const jwt = require('jsonwebtoken')
exports.regUser = (req, res) => {
    let data = Object.keys(req.body) + '';
    data = JSON.parse(data);
    // 检测用户名是否被占用
    const sqlStr = 'select * from user where uname=?';
    db.query(sqlStr, data.uname, (err, resules) => {
        if (err) {
            return res.send({ status: 1, message: err.message });
        }
        if (resules.length > 0) {
            return res.send({ status: 1, message: '用户名被占用' });
        }
        const sql = 'insert into user set ?';
        db.query(sql, { uname: data.uname, upas: data.upas }, function (err, results) {
            if (err) return res.send({ status: 1, message: err.message })
            if (results.affectedRows !== 1) { return res.send({ status: 1, message: '注册用户失败，请稍后再试！' }) }
            res.send({ status: 0, message: '注册成功！' })
        })
    })
    // 插入用户
};
exports.login = (req, res) => {
    let data = Object.keys(req.body) + '';
    data = JSON.parse(data);
    res.data = data;
    const userinfo = res.data;
    const sql = `select * from user where uname=?`
    console.log(userinfo);
    db.query(sql, userinfo.uname, function (err, results) {
        if (err) return res.send({ status: 1, message: err.message });
        if (results.length !== 1) return res.send({ status: 1, message: "不存在此用户" })
        // const compareResult = bcrypt.compareSync(userinfo.upas, results[0].upas)
        console.log(userinfo.upas);
        console.log(results[0].upas);
        // console.log(compareResult);
        if (userinfo.upas !== results[0].upas) return res.send({ status: 1, message: "账号密码错误" });
        const user = { ...results[0], password: '', user_pic: '' }
        const tokenStr = jwt.sign(user, config.jwtSecretKey, {
            expiresIn: config.expiresIn,
        })
        res.send({
            status: 0,
            message: '登录成功！', // 为了方便客户端使用 Token，在服务器端直接拼接上 Bearer 的前缀
            token: 'Bearer ' + tokenStr,
        })
    })
};
exports.login_teacher = (req, res) => {
    let data = Object.keys(req.body) + '';
    data = JSON.parse(data);
    res.data = data;
    const userinfo = res.data;
    const sql = `select * from teacher where uname=?`
    console.log(userinfo);
    db.query(sql, userinfo.uname, function (err, results) {
        if (err) return res.send({ status: 1, message: err.message });
        if (results.length !== 1) return res.send({ status: 1, message: "不存在此用户" })
        // const compareResult = bcrypt.compareSync(userinfo.upas, results[0].upas)
        console.log(userinfo.upas);
        console.log(results[0].upas);
        // console.log(compareResult);
        if (userinfo.upas !== results[0].upas) return res.send({ status: 1, message: "账号密码错误" });
        const user = { ...results[0], password: '', user_pic: '' }
        const tokenStr = jwt.sign(user, config.jwtSecretKey, {
            expiresIn: config.expiresIn,
        })
        res.send({
            status: 0,
            message: '登录成功！', // 为了方便客户端使用 Token，在服务器端直接拼接上 Bearer 的前缀
            token: 'Bearer ' + tokenStr,
        })
    })
};
exports.add_homework_teacher = (req, res) => {
    let data = Object.keys(req.body) + '';
    data = JSON.parse(data);
    // 检测用户名是否被占用
    console.log(data);
    const homework = JSON.stringify(data.data)
    console.log(homework);
    const sqlStr = 'select * from homework where title=?';
    console.log(data.main_title);
    db.query(sqlStr, data.main_title, (err, resules) => {
        if (err) {
            return res.send({ status: 1, message: err.message });
        }
        if (resules.length > 0) {
            return res.send({ status: 1, message: '此试卷已添加' });
        }
        const sql = 'insert into homework set ?';
        // console.log(data.data[0].title);
        console.log(data.main_title);
        db.query(sql, { title: data.main_title, uname: 'teacher', homework: homework }, function (err, results) {
            if (err) return res.send({ status: 1, message: err.message })
            if (results.affectedRows !== 1) { return res.send({ status: 1, message: '此' }) }
            res.send({ status: 0, message: '添加成功！' })
        })
    })
};


exports.get_homework_titile_student = (req, res) => {
    const sqlStr = 'select title from homework where uname = ?';
    teacher_name = "teacher"
    db.query(sqlStr, teacher_name, (err, resules) => {
        if (err) {
            return res.send({ status: 1, message: err.message });
        }

        // console.log(resules);
        res.send(resules)
    })
};
exports.get_homework_student = (req, res) => {
    let data = Object.keys(req.body) + '';
    // data = JSON.parse(data);
    const sqlStr = 'select * from homework where title=?';
    db.query(sqlStr, data, (err, resules) => {
        if (err) {
            return res.send({ status: 1, message: err.message });
        }
        // console.log(resules[0]  );
        res.send(resules[0])
    })



    // const sqlStr = 'select title from homework';
    // db.query(sqlStr, (err, resules) => {
    //     if (err) {
    //         return res.send({ status: 1, message: err.message });
    //     }

    //     console.log(resules);
    //     res.send(resules)
    // })
};
exports.get_uname = (req, res) => {
    console.log(req.body);
    res.send(req.body)
}
